package eu.boss.fragments;

import java.util.List;

import Util.Util;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
import eu.boss.R;
import eu.boss.activities.FragmentsSliderActivity;
import eu.boss.model.Player;

/**
 * Fragment displayed on the right. Contains a listview with a checkbox to select players and add a new game
 * @author Arnaud
 *
 */

public class GameFragment extends Fragment{
	
	/**
	 * @param mMapper Mapper to map JSON data
	 * @param mLvPlayers ListView containing datas
	 * @param mCheckedState Array containing checkbox states
	 * @param mControllerConnection Connection with the service 
	 */
	private ListView mLvPlayers;
	private boolean[] mCheckedState;
	private FragmentsSliderActivity sliderActivity;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v=inflater.inflate(R.layout.gamelist,container, false);
		sliderActivity = (FragmentsSliderActivity) getActivity();
		mLvPlayers = (ListView) v.findViewById(R.id.lvGameListPlayers);
		initiate();
		return v;
	}


	/**
	 * Initialize components
	 */
	public void initiate() {
		try
		{
			mCheckedState = new boolean[sliderActivity.getController().getStatsMessage().getPlayers().size()];
		}
		catch (NullPointerException e)
		{
			mCheckedState= new boolean[0];
		}
		updateList();
	}
	
	
	/**
	 * Update list with the adapter
	 */
	public void updateList() {
		GameAdapter adapter;
		try{
			mCheckedState = new boolean[sliderActivity.getController().getStatsMessage().getPlayers().size()];
			adapter = new GameAdapter(sliderActivity.getController().getStatsMessage().getPlayers());
			mLvPlayers.setAdapter(adapter);
		}
		catch (NullPointerException e)
		{
			Log.e("NullPointerException", "NullPointerException");
		}
	}
	

	/**
	 *  A pretty basic ViewHolder used to keep references on children
	 *  
	 * @author Arnaud
	 *
	 */
	private static class ViewHolder {
		TextView tvName;
		TextView tvColor;
		CheckBox cbGame;
	}
	
	/**
	 * Adapter to fill the listview with custom items containing 2 textview and a checkbox.
	 * @author Arnaud
	 *
	 */
	private class GameAdapter extends BaseAdapter {

		private List<Player> players;

		public GameAdapter(List<Player> p) {
			players = p;
		}

		public int getCount() {
			return players.size();
		}

		public Object getItem(int position) {
			return players.get(position);
		}

		public long getItemId(int position) {
			return position;
		}
		

		/**
		 * This method fill every single item included in the listview
		 */

		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;

			if (convertView == null) {

				// initialize components
				convertView = getActivity().getLayoutInflater().inflate(R.layout.listviewitemgame, null);
				holder = new ViewHolder();				
				holder.tvName = (TextView) convertView.findViewById(R.id.tvNameGame);
				holder.cbGame = (CheckBox) convertView.findViewById(R.id.checkBoxGame);
				holder.tvColor = (TextView) convertView.findViewById(R.id.tvColorGame);
				
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			//to fix checkbox bug
			holder.cbGame.setOnCheckedChangeListener(null);
            holder.cbGame.setChecked(mCheckedState[position]);
            holder.cbGame.setOnCheckedChangeListener(mCheckedChanceChangeListener);
            
            // fill component
			holder.tvName.setText(players.get(position).getName());
			holder.tvColor.setBackgroundDrawable(Util.getDrawableColor(getActivity(), players.get(position).getColor()));
			return convertView;
		}
	}

	private OnCheckedChangeListener mCheckedChanceChangeListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// Keeping checkbox state in the array
			final int position = mLvPlayers.getPositionForView(buttonView);
			if (position != ListView.INVALID_POSITION) {
				mCheckedState[position] = isChecked;
			}
		}
	};

	public boolean[] getCheckedState() {
		return mCheckedState;
	}

	
}
